package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.common.Result;
import com.itheima.health.mapper.MemberMapper;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderDto;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.xml.crypto.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import static com.itheima.health.pojo.Order.ORDERSTATUS_NO;
import static com.itheima.health.pojo.Order.ORDERTYPE_WEIXIN;

/**
 * @author cfr
 * @date 2022/1/6
 * @TIME:20:53
 */
@Service
@EnableTransactionManagement
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Result submit(OrderDto orderDto) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getPhoneNumber, orderDto.getTelephone());
        Member member = memberMapper.selectOne(wrapper);
        member.setName(orderDto.getName());
        member.setIdCard(orderDto.getIdCard());
        member.setSex(orderDto.getSex());
        memberMapper.updateById(member);

        //判断时间
        LambdaQueryWrapper<Order> wrapper2 = new LambdaQueryWrapper<>();
        try {
            wrapper2.eq(Order::getMemberId, member.getId())
                    .eq(Order::getOrderDate,simpleDateFormat.parse(simpleDateFormat.format(orderDto.getOrderDate())) );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Order order = orderMapper.selectOne(wrapper2);
        System.out.println(order);
       if(order!=null){
               return new Result(false, "已预约");
       }
        //新增预约
        Order order1 = new Order();
        order1.setMemberId(member.getId());
        order1.setOrderDate(orderDto.getOrderDate());
        order1.setOrderType(ORDERTYPE_WEIXIN);
        order1.setOrderStatus(ORDERSTATUS_NO);
        order1.setSetmealId(orderDto.getSetmealId());
        orderMapper.insert(order1);

        LambdaQueryWrapper<OrderSetting> wrapper3 = new LambdaQueryWrapper<>();
        try {
            wrapper3.eq(OrderSetting::getOrderDate,simpleDateFormat.parse( simpleDateFormat.format(orderDto.getOrderDate())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        OrderSetting orderSetting = orderSettingMapper.selectOne(wrapper3);

        if(orderSetting!=null){
            if(orderSetting.getNumber()==orderSetting.getReservations()){
                return new Result(false, "预约人数已满");
            }
            orderSetting.setReservations(orderSetting.getReservations()+1);
            orderSettingMapper.updateById(orderSetting);
            return new Result(true, "预约成功" );
        }
        OrderSetting orderSetting1 = new OrderSetting();
        orderSetting1.setOrderDate(orderDto.getOrderDate());
        orderSetting1.setNumber(300);
        orderSetting1.setReservations(1);
        orderSettingMapper.insert(orderSetting1);
        return new Result(true, "预约成功");
    }
}
